package cn.itcast.base;

import cn.itcast.domain.User;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;

public class BaseContorller {
    public HttpServletRequest request;
    public HttpServletResponse response;
    public HttpSession session;
    public String company_id;
    public String company_name;
    public User user;
    /*contorller的方法之前执行的*/
    @ModelAttribute
    public void init(HttpServletRequest request, HttpSession session, HttpServletResponse response){
        this.request=request;
        this.response=response;
        this.session=session;
        /*获取当前登录的用户*/
        User loginUser = (User)session.getAttribute("loginUser");
        if (loginUser!=null){
            this.company_id=loginUser.getCompanyId(); //后期改成当前登录用户的
            this.company_name=loginUser.getCompanyName(); //后期改成当前登录用户的
        }
        this.user=loginUser;//当前登录的对象

    }
    /*获取exce 单元格的数据*/
    public Object getCellValue(Cell cell) {
        Object obj=null;
        //判断单元格类型
        CellType cellType = cell.getCellType();
        switch (cellType){
            case STRING:
                obj=cell.getStringCellValue();
                break;
            case BOOLEAN:
                obj= cell.getBooleanCellValue();
                break;
            case NUMERIC:
                boolean flg = DateUtil.isCellDateFormatted(cell);
                if(flg){
                    //日期类型
                    obj=cell.getDateCellValue();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    obj=format.format(obj);
                }else{
                    obj=cell.getNumericCellValue();
                }
        }
        return obj;
    }
}
